package com.hznu.jspFinal.controller;


import com.hznu.jspFinal.entity.AttendExam;
import com.hznu.jspFinal.entity.User;
import com.hznu.jspFinal.myBeans.R;
import com.hznu.jspFinal.service.AttendExamService;
import com.hznu.jspFinal.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Api(description = "参加考试接口",value = "AttendExam")
@RestController
@RequestMapping("/AttendExam")
public class AttendExamController {

    @Autowired
    AttendExamService attendExamService;

    @Autowired
    UserService userService;

    @ApiOperation(value = "批量添加考试学生")
    @PostMapping("/addBatch")
    public R addBatch(@RequestParam Integer examId,@RequestParam(value="studentIdList[]")Integer[] studentIdList)
    {
        try
        {
            attendExamService.addBatch(examId,studentIdList);
            return R.ok();
        }catch(Exception e)
        {
            e.printStackTrace();
            return R.error();
        }
    }

    @ApiOperation(value="通过考试id查找学生")
    @PostMapping("/findStudentListByExamId/{examid}")
    public R findStudentListByExamId(@PathVariable Integer examid)
    {
        try
        {
            AttendExam attendExam = new AttendExam();
            attendExam.setExamid(examid);
            List<AttendExam> attendExams = attendExamService.findByCondition(attendExam);
            List<User> users = new ArrayList<>();
            for(AttendExam attendExam1 : attendExams)
            {
                users.add(userService.findByUserId(attendExam1.getStudentid()));
            }

            return R.ok().put("data",users);
        }catch(Exception e)
        {
            e.printStackTrace();
            return R.error();
        }
    }

}
